package com.qingke.mybatis.mapping;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.qingke.pojo.Blog;
import com.qingke.pojo.User;

public interface BlogMapper {

	@Insert("insert into blog(userId, title, content) values(#{userId}, #{title}, #{content});")
	@Options(keyProperty="id", useGeneratedKeys=true)
	void insertBlog(Blog blog);
	
	@Update("update blog set userId = #{userId}, title = #{title}, content = #{content} where id = #{id};")
	void updateBlog(Blog blog);
	
	@Delete("delete from blog where id = #{id}")
	void deleteBlog(Blog blog);
	
	@Select("select * from blog")
	List<Blog> selectAllBlogs();
	
	@Select("select * from blog where userId = #{userId}")
	List<Blog> selectBlogsByUserId(int userId);
	
	@Select("select * from blog where id = #{id}")
	Blog selectBlogById(int id);
	
	@Select("select * from blog where id = #{id}")
	@Results(value = {
			@Result(property = "id", column = "id"),
			@Result(property = "userId", column = "userId"),
			@Result(property = "title", column = "title"),
			@Result(property = "content", column = "content"),
			@Result(property = "author", column = "userId", javaType = User.class,
					one = @One(select = "com.qingke.mybatis.mapping.userMapper.selectById")),
	})
	Blog findBlog2ById(int id);
}
